/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ortustve.util;

import java.util.HashMap;
import java.util.Map;
import java.lang.String;
import java.lang.Math;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.text.SimpleDateFormat;

import sagex.api.Global;
import sagex.api.Configuration;
import sagex.api.MediaFileAPI;
import sagex.api.Utility;
import sagex.api.AiringAPI;
/**
 *
 * @author Aaron
 */
public class ui extends ortustve.ortusvars {
	/*
	 * Given a position relative to the full visible screen will return the position taking into account overscan.
	 * Good for "Background Components" that need to line up with non background panel widgets.
	 * @param A - the UI position (0.00 - 1.00)
	 * @param wrt0 - true = A is wrt 0 (top), false = A is wrt 1 (bottom)
	 */
	private static final String MOVIES = "Movies";
	private static final long msperday = 86400000;


	public static double GetVerticalPositionWithOverscan(double A, boolean wrt0)
	{
		double B = 0;

		if (A < 0) {
			A = 0;
		}
		if (A > 1.0) {
			A = 1.0;
		}

		double Dim = Global.GetFullUIHeight(ortustve.api.GetUIContext());
		double Offset = Configuration.GetOverscanOffsetY(ortustve.api.GetUIContext());
		double Scale = Configuration.GetOverscanScaleHeight(ortustve.api.GetUIContext());

		if (wrt0) {
			B = Offset / Dim + A * Scale;
		} else {
			B = 1 - (Offset / Dim + (1 - A) * Scale);
		}

		if (B < 0) {
			B = 0;
		}
		if (B > 1.0) {
			B = 1.0;
		}

//		ortus.api.DebugLog(TRACE, "Vertical Position With Overscan");
//		ortus.api.DebugLog(TRACE, "Input = '" + A + "'");
//		ortus.api.DebugLog(TRACE, "FullUIWidth = '" + Dim + "'");
//		ortus.api.DebugLog(TRACE, "OverscanOffsetX = '" + Offset + "'");
//		ortus.api.DebugLog(TRACE, "OverscanScaleWidth = '" + Scale + "'");
//		ortus.api.DebugLog(TRACE, "Output = '" + B + "'");

		return B;
	}

	/*
	 * Given a relative position will return the true position taking into account overscan.
	 * Good for "Background Components" that need to line up with non background panel widgets.
	 * @param A - the UI position (0.00 - 1.00)
	 * @param wrt0 - true = A is wrt 0 (left), false = A is wrt 1 (right)
	 */
	public static double GetHorizontalPositionWithOverscan(double A, boolean wrt0)
	{
		double B = 0;

		if (A < 0) {
			A = 0;
		}
		if (A > 1.0) {
			A = 1.0;
		}

		double Dim = Global.GetFullUIWidth(ortustve.api.GetUIContext());
		double Offset = Configuration.GetOverscanOffsetX(ortustve.api.GetUIContext());
		double Scale = Configuration.GetOverscanScaleWidth(ortustve.api.GetUIContext());

		if (wrt0) {
			B = Offset / Dim + A * Scale;
		} else {
			B = 1 - (Offset / Dim + (1 - A) * Scale);
		}

		if (B < 0) {
			B = 0;
		}
		if (B > 1.0) {
			B = 1.0;
		}

//		ortus.api.DebugLog(TRACE, "Horizontal Pos With Overscan");
//		ortus.api.DebugLog(TRACE, "Input = '" + A + "'");
//		ortus.api.DebugLog(TRACE, "FullUIWidth = '" + Dim + "'");
//		ortus.api.DebugLog(TRACE, "OverscanOffsetX = '" + Offset + "'");
//		ortus.api.DebugLog(TRACE, "OverscanScaleWidth = '" + Scale + "'");
//		ortus.api.DebugLog(TRACE, "Output = '" + B + "'");
		return B;
	}

	public static Object GetMediaObject(Object o1, Object o2) {
		if (MediaFileAPI.IsMediaFileObject(o1))
			return o1;

		if ( o1 instanceof String) {
			return ((List)((HashMap)o2).get(o1)).get(0);
		} else
			return null;
	}

	public static List InsertDividers(Map map, List<String> keys, String type){
		ArrayList<String> keysnew = new ArrayList(keys);

		String DividerA = "";
		String DividerB = "";
		for (String key : keys){
			DividerB = GetDivider(map, key, type);
//			ortustve.api.DebugLog(TRACE, "DividerA = " + DividerA);
//			ortustve.api.DebugLog(TRACE, "DividerB = " + DividerB);
			if (!(DividerB.equals(DividerA))){
//				ortustve.api.DebugLog(TRACE, "DividerB = DividerA; inserting Divider");
				keysnew.add(keysnew.indexOf(key), DividerB);
				DividerA = DividerB;
			}
		}
		return keysnew;
	}

	public static List InsertDividers(Map map, String type){
		ArrayList<String> keys = new ArrayList(map.keySet());
		return InsertDividers(map,keys,type);
	}

	public static String GetDivider(Map map, String key, String type){

		key = ParseDivider(key);

		if (key.equalsIgnoreCase("Movies"))
			return DIVIDER.concat(MOVIES);
		Object mf = Utility.GetElement(Utility.GetSubgroup(map, key), 0);
		if (type.equalsIgnoreCase("Date")){
			return DIVIDER.concat(GetDividerDate(mf));
		}
		return DIVIDER.concat(key);
	}

	public static boolean IsKeyDivider(Map map, String key, String type){
		List keys = ortustve.util.array.GetMapKeysAsList(map);
		int key_i = keys.indexOf(key);
		if(key_i == 0)
			return true;

		String key_prev = (String)keys.get(key_i-1);
		if(GetDivider(map,key,type).equals(GetDivider(map,key_prev,type)))
			return false;
		else
			return true;

	}

	public static boolean IsDivider(String str){
		if (str == null || str.trim().equals(""))
			return false;
		return str.startsWith(DIVIDER);
	}

	public static String ParseDivider(String str){
		return str.replaceAll(DIVIDER, "");
	}

	public static String GetDividerText(String divider){
		return ParseDivider(divider);
	}

	public static String GetDividerDate(Object mf){

		SimpleDateFormat sdfDetailed = new SimpleDateFormat("EEEE, M/d/yy");
		SimpleDateFormat sdfCompare = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sdfMonth = new SimpleDateFormat("MMMM yyyy");
		SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
//		SimpleDateFormat sdfFull = new SimpleDateFormat("yyyyMMdd HHmmss");
		
		long mfTime;
		long todayTime;
		try {
			mfTime = sdfCompare.parse(sdfCompare.format(AiringAPI.GetAiringStartTime(mf))).getTime();
		}
		catch (Exception e){
			mfTime = 0;
		}
		try {
			todayTime = sdfCompare.parse(sdfCompare.format(System.currentTimeMillis())).getTime();
		}
		catch (Exception e){
			todayTime = 0;
		}
//		int mfDay =  Integer.parseInt(sdfCompare.format(mfTime));
//		int todayDay = (int) Integer.parseInt(sdfCompare.format(todayTime));
		int mfDay = (int) (mfTime / msperday);
		int todayDay = (int) (todayTime / msperday);




		int Diff = mfDay - todayDay;

//		ortustve.api.DebugLog(TRACE, AiringAPI.GetAiringTitle(mf) + " DIff = " + Diff + ", msDiff = " + (mfTime - todayTime));
//		ortustve.api.DebugLog(TRACE, "  " + sdfFull.format(mfTime) + ", mfTime = " + mfTime + ", mfDay = " + mfDay);
//		ortustve.api.DebugLog(TRACE, "  " + sdfFull.format(todayTime) + ", todayTime = " + todayTime + ", TodayDay = " + todayDay);


		switch (Diff) {
			case -1: return "Yesterday";
			case 0: return "Today";
			case 1: return "Tomorrow";
		}
		if (Diff < 0){
			if (Math.abs(Diff) > 180)
				return sdfYear.format(mfTime);

			if (Math.abs(Diff) > 28)
				return sdfMonth.format(mfTime);
		}
		return sdfDetailed.format(mfTime);
	}
}
